# 加载必要的库
Add-Type -Path "C:\path\to\HtmlAgilityPack.dll" # 替换为HtmlAgilityPack的实际路径
Import-Module DocumentFormat.OpenXml

# 获取网页内容
$url = "https://www.news.cn/politics/leaders/20241212/f47e778630ec4ff6b51c99d55cef6f43/c.html"  # 将此URL替换为你想要抓取的网页地址
$response = Invoke-WebRequest -Uri $url
$htmlContent = $response.Content

# 解析HTML并提取<p>标签的内容
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($htmlContent)
$pTags = $doc.DocumentNode.SelectNodes("//p")

# 创建Word文档
$wordFilePath = "D:\中央经济工作会议精神.docx"
$wordApp = New-Object -ComObject Word.Application
$wordApp.Visible = $false
$docx = $wordApp.Documents.Add()

# 添加<p>标签的内容到Word文档
foreach ($p in $pTags) {
    if ($p.InnerText.Trim() -ne "") {
        $docx.Content.InsertAfter($p.InnerText + "`r`n")
    }
}

# 保存并关闭Word文档
$docx.SaveAs([ref]$wordFilePath)
$docx.Close()
$wordApp.Quit()

# 释放COM对象
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($docx) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wordApp) | Out-Null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()

Write-Host "文档已成功创建: $wordFilePath"